#pragma once
#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_err.h"
#include "nvs_flash.h"
#include "protocol_examples_common.h"

#include "esp_task_wdt.h"
#include "esp_wifi.h"
#include "string.h"
#include "esp_mac.h"
#include "esp_http_server.h"
#include "stdio.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "freertos/task.h"
//OTA
#include "esp_system.h"
#include "freertos/event_groups.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "esp_https_ota.h"
#include "esp_ota_ops.h"
#include "esp_http_client.h"
#include "esp_crt_bundle.h"

static const char *TAG = "WiFikey";

#define CONFIG_GPIO_OUTPUT_0 18
#define CONFIG_GPIO_OUTPUT_1 19
#define CONFIG_GPIO_OUTPUT_2 15
#define ENGINE_PIN 16
#define GPIO_OUTPUT_IO_0    CONFIG_GPIO_OUTPUT_0
#define GPIO_OUTPUT_IO_1    CONFIG_GPIO_OUTPUT_1
#define GPIO_OUTPUT_IO_2    CONFIG_GPIO_OUTPUT_2
#define GPIO_OUTPUT_PIN_SEL  ((1ULL<<GPIO_OUTPUT_IO_0) | (1ULL<<GPIO_OUTPUT_IO_1) | (1ULL<<GPIO_OUTPUT_IO_2) | (1ULL<<ENGINE_PIN))

//WIFI part
#define ESP_WIFI_SSID "Godric"
#define ESP_WIFI_PASSWORD "gg654321"
#define CONFIG_ESP_WIFI_PW_ID ""

#define ESP_WIFI_CHANNEL 7
#define ESP_MAX_STA_CONN 2

// const TickType_t xDelay = 500 / portTICK_PERIOD_MS;

static EventGroupHandle_t s_wifi_event_group;
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT      BIT1
#define CONFIG_ESP_MAXIMUM_RETRY 5

//4G module uart
#define UART_4G_NUM UART_NUM_1
#define UART1_TX_NUM 22
#define UART1_RX_NUM 23


static int pwrstate = -1;

#define BUF_SIZE (1024)
#define RD_BUF_SIZE (BUF_SIZE)
static QueueHandle_t uart1_queue;
static QueueHandle_t uart2_queue;
static int OTA_UPDATE=0;

static int s_retry_num = 0;

static bool dooropened = false;
static void configure_led(const char *TAG)
{
    ESP_LOGE(TAG, "configuring GPIO 15, 18 and 19 as OUTPUT...");
    //zero-initialize the config structure.
    gpio_config_t io_conf = {};
    //disable interrupt
    io_conf.intr_type = GPIO_INTR_DISABLE;
    //set as output mode
    io_conf.mode = GPIO_MODE_OUTPUT;
    //bit mask of the pins that you want to set,e.g.GPIO18/19
    io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
    //disable pull-down mode
    io_conf.pull_down_en = 0;
    //disable pull-up mode
    io_conf.pull_up_en = 0;
    //configure GPIO with the given settings
    gpio_config(&io_conf);
}